home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / tcshsrc.zoo / tcsh / ed.refresh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  28.6 KB  |  1,173 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.00/RCS/ed.refresh.c,v 3.1 1991/07/15 19:37:24 christos Exp $ */
  2. /*
  3.  * ed.refresh.c: Lower level screen refreshing functions
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "config.h"
  38. RCSID("$Id: ed.refresh.c,v 3.1 1991/07/15 19:37:24 christos Exp $")
  39.  
  40. #include "sh.h"
  41. #include "ed.h"
  42. /* #define DEBUG_UPDATE */
  43. /* #define DEBUG_REFRESH */
  44. /* #define DEBUG_LITERAL */
  45.  
  46. /* refresh.c -- refresh the current set of lines on the screen */
  47.  
  48. Char   *litptr[256];
  49. static int vcursor_h, vcursor_v;
  50.  
  51. static    void    Draw             __P((int));
  52. static    void    Vdraw             __P((int));
  53. static    void    Vnewline         __P((void));
  54. static    void    update_line         __P((Char *, Char *, int));
  55. static    void    str_insert        __P((Char *, int, int, Char *, int));
  56. static    void    str_delete        __P((Char *, int, int, int));
  57. static    void    str_cp            __P((Char *, Char *, int));
  58. static    void    PutPlusOne        __P((int));
  59. static    void    cpy_pad_spaces        __P((Char *, Char *, int));
  60. #ifdef DEBUG_UPDATE
  61. static    void    dprintstr        __P((char *, Char *, Char *));
  62. static    void    dprintf            __P((char *, ...));
  63. #endif  /* DEBUG_UPDATE */
  64.  
  65. #ifdef DEBUG_UPDATE
  66. static void
  67. dprintstr(str, f, t)
  68. char *str;
  69. Char *f, *t;
  70. {
  71.     dprintf("%s:\"", str);
  72.     while (f < t)
  73.     dprintf("%c", *f++ & ASCII);
  74.     dprintf("\"\r\n");
  75.  
  76. /* dprintf():
  77.  *    Print to $DEBUGTTY, so that we can test editing on one pty, and 
  78.  *      print debugging stuff on another. Don't interrupt the shell while
  79.  *    debugging cause you'll mangle up the file descriptors!
  80.  */
  81. static void
  82. #if __STDC__
  83. dprintf(char *fmt, ...)
  84. #else
  85. dprintf(va_list)
  86.     va_dcl
  87. #endif /* __STDC__ */
  88. {
  89.     static int fd = -1;
  90.     char *dtty;
  91.  
  92.     if ((dtty = getenv("DEBUGTTY"))) {
  93.     int o;
  94.     va_list va;
  95. #if __STDC__
  96.     va_start(va, fmt);
  97. #else
  98.     char *fmt;
  99.     va_start(va);
  100.     fmt = va_arg(va, char *);
  101. #endif /* __STDC__ */
  102.  
  103.     if (fd == -1)
  104.         fd = open(dtty, O_RDWR);
  105.     o = SHOUT;
  106.     flush();
  107.     SHOUT = fd;
  108.     vprintf(fmt, va);
  109.     va_end(va);
  110.     flush();
  111.     SHOUT = o;
  112.     }
  113. }
  114. #endif  /* DEBUG_UPDATE */
  115.  
  116. static void
  117. Draw(c)                /* draw c, expand tabs, ctl chars */
  118.     register int c;
  119. {
  120.     register Char ch = c & CHAR;
  121.  
  122.     if (Isprint(ch)) {
  123.     Vdraw(c);
  124.     return;
  125.     }
  126.     /* from wolman%crltrx.DEC@decwrl.dec.com (Alec Wolman) */
  127.     if (ch == '\n') {        /* expand the newline     */
  128.     Vdraw('\0');    /* assure end of line     */
  129.     vcursor_h = 0;        /* reset cursor pos     */
  130.     vcursor_v++;
  131.     return;
  132.     }
  133.     if (ch == '\t') {        /* expand the tab      */
  134.     for (;;) {
  135.         Vdraw(' ');
  136.         if ((vcursor_h & 07) == 0)
  137.         break;        /* go until tab stop     */
  138.     }
  139.     }
  140.     else if (Iscntrl(ch)) {
  141.     Vdraw('^');
  142.     if (ch == '\177') {
  143.         Vdraw('?');
  144.     }
  145.     else {
  146.         /* uncontrolify it; works only for iso8859-1 like sets */
  147.         Vdraw((c | 0100));
  148.     }
  149.     }
  150.     else {
  151.     Vdraw('\\');
  152.     Vdraw(((c >> 6) & 7) + '0');
  153.     Vdraw(((c >> 3) & 7) + '0');
  154.     Vdraw((c & 7) + '0');
  155.     }
  156. }
  157.  
  158. static void
  159. Vdraw(c)            /* draw char c onto V lines */
  160.     register int c;
  161. {
  162. #ifdef DEBUG_REFRESH
  163. # ifdef SHORT_STRINGS
  164.     dprintf("Vdrawing %6.6o '%c'\r\n", c, c & ASCII);
  165. # else
  166.     dprintf("Vdrawing %3.3o '%c'\r\n", c, c);
  167. # endif /* SHORT_STRNGS */
  168. #endif  /* DEBUG_REFRESH */
  169.  
  170.     Vdisplay[vcursor_v][vcursor_h] = c;
  171.     vcursor_h++;        /* advance to next place */
  172.     if (vcursor_h >= TermH) {
  173.     Vdisplay[vcursor_v][TermH] = '\0';    /* assure end of line */
  174.     vcursor_h = 0;        /* reset it. */
  175.     vcursor_v++;
  176.     if (vcursor_v >= TermV) {    /* should NEVER happen. */
  177. #ifdef DEBUG_REFRESH
  178.         dprintf("\r\nVdraw: vcursor_v overflow! Vcursor_v == %d > %d\r\n",
  179.             vcursor_v, TermV);
  180.         abort();
  181. #endif /* DEBUG_REFRESH */
  182.     }
  183.     }
  184. }
  185.  
  186. static void
  187. Vnewline()
  188. {
  189.     /* needs work. */
  190. }
  191.  
  192. /*
  193.  *  Refresh()
  194.  *    draws the new virtual screen image from the current input
  195.  *      line, then goes line-by-line changing the real image to the new
  196.  *    virtual image. The routine to re-draw a line can be replaced
  197.  *    easily in hopes of a smarter one being placed there.
  198.  */
  199. static int OldvcV = 0;
  200. void
  201. Refresh()
  202. {
  203.     register int cur_line;
  204.     register Char *cp;
  205.     int     cur_h, cur_v = 0, new_vcv;
  206.     Char    oldgetting;
  207.     int     litnum = 0;
  208.  
  209. #ifdef DEBUG_REFRESH
  210.     dprintf("PromptBuf = :%s:\r\n", short2str(PromptBuf));
  211.     dprintf("InputBuf = :%s:\r\n", short2str(InputBuf));
  212. #endif /* DEBUG_REFRESH */
  213.     oldgetting = GettingInput;
  214.     GettingInput = 0;        /* avoid re-entrance via SIGWINCH */
  215.  
  216.     /* reset the Vdraw cursor */
  217.     vcursor_h = 0;
  218.     vcursor_v = 0;
  219.  
  220.     /* draw prompt, we know it's ASCIZ */
  221.     for (cp = PromptBuf; *cp; cp++) {
  222.     if (*cp & LITERAL) {
  223.         if (litnum < (sizeof(litptr) / sizeof(litptr[0]))) {
  224.         litptr[litnum] = cp;
  225. #ifdef DEBUG_LITERAL
  226.         dprintf("litnum = %d, litptr = %x:\r\n",
  227.             litnum, litptr[litnum]);
  228. #endif /* DEBUG_LITERAL */
  229.         }
  230.         while (*cp & LITERAL)
  231.         cp++;
  232.         if (*cp)
  233.         Vdraw(litnum++ | LITERAL);
  234.         else {
  235.         /*
  236.          * XXX: This is a bug, we lose the last literal, if it is not
  237.          * followed by a normal character, but it is too hard to fix
  238.          */
  239.         break;
  240.         }
  241.     }
  242.     else
  243.         Draw(*cp);
  244.     }
  245.     cur_h = -1;            /* set flag in case I'm not set */
  246.  
  247.     /* draw the current input buffer */
  248.     for (cp = InputBuf; (cp < LastChar); cp++) {
  249.     if (cp == Cursor) {
  250.         cur_h = vcursor_h;    /* save for later */
  251.         cur_v = vcursor_v;
  252.     }
  253.     Draw(*cp);
  254.     }
  255.  
  256.     /* to next line and draw the current search prompt if searching */
  257.     if (DoingSearch) {
  258.     Vnewline();
  259.     for (cp = SearchPrompt; *cp; cp++)
  260.         Draw(*cp);
  261.     for (cp = InputBuf; (cp < LastChar); cp++) {
  262.         if (cp == Cursor) {
  263.         cur_h = vcursor_h;    /* save for later */
  264.         cur_v = vcursor_v;
  265.         }
  266.         Draw(*cp);
  267.     }
  268.     }
  269.  
  270.     if (cur_h == -1) {        /* if I havn't been set yet, I'm at the end */
  271.     cur_h = vcursor_h;
  272.     cur_v = vcursor_v;
  273.     }
  274.     new_vcv = vcursor_v;    /* must be done BEFORE the NUL is written */
  275.     Vdraw('\0');        /* put NUL on end */
  276.  
  277. #ifdef DEBUG_REFRESH
  278.     dprintf("TermH=%d, vcur_h=%d, vcur_v=%d, Vdisplay[0]=\r\n:%80.80s:\r\n",
  279.         TermH, vcursor_h, vcursor_v, short2str(Vdisplay[0]));
  280. #endif /* DEBUG_REFRESH */
  281.  
  282. #ifdef DEBUG_UPDATE
  283.     dprintf("updating %d lines.\r\n", new_vcv);
  284. #endif  /* DEBUG_UPDATE */
  285.     for (cur_line = 0; cur_line <= new_vcv; cur_line++) {
  286.     /* NOTE THAT update_line MAY CHANGE Display[cur_line] */
  287.     update_line(Display[cur_line], Vdisplay[cur_line], cur_line);
  288.  
  289.     /*
  290.      * Copy the new line to be the current one, and pad out with spaces
  291.      * to the full width of the terminal so that if we try moving the
  292.      * cursor by writing the character that is at the end of the
  293.      * screen line, it won't be a NUL or some old leftover stuff.
  294.      */
  295.     cpy_pad_spaces(Display[cur_line], Vdisplay[cur_line], TermH);
  296. #ifdef notdef
  297.     (void) Strncpy(Display[cur_line], Vdisplay[cur_line], TermH);
  298.     Display[cur_line][TermH] = '\0';    /* just in case */
  299. #endif
  300.     }
  301. #ifdef DEBUG_REFRESH
  302.     dprintf("\r\nvcursor_v = %d, OldvcV = %d, cur_line = %d\r\n",
  303.         vcursor_v, OldvcV, cur_line);
  304. #endif /* DEBUG_REFRESH */
  305.     if (OldvcV > new_vcv) {
  306.     for (; cur_line <= OldvcV; cur_line++) {
  307.         MoveToLine(cur_line);
  308.         MoveToChar(0);
  309.         ClearEOL(Strlen(Display[cur_line]));
  310. #ifdef DEBUG_REFRESH
  311.         so_write(str2short("C\b"), 2);
  312. #endif /* DEBUG_REFRESH */
  313.         *Display[cur_line] = '\0';
  314.     }
  315.     }
  316.     OldvcV = new_vcv;        /* set for next time */
  317. #ifdef DEBUG_REFRESH
  318.     dprintf("\r\nCursorH = %d, CursorV = %d, cur_h = %d, cur_v = %d\r\n",
  319.         CursorH, CursorV, cur_h, cur_v);
  320. #endif /* DEBUG_REFRESH */
  321.     MoveToLine(cur_v);        /* go to where the cursor is */
  322.     MoveToChar(cur_h);
  323.     SetAttributes(0);        /* Clear all attributes */
  324.     flush();            /* send the output... */
  325.     GettingInput = oldgetting;    /* reset to old value */
  326. }
  327.  
  328. #ifdef notdef
  329. GotoBottom()
  330. {                /* used to go to last used screen line */
  331.     MoveToLine(OldvcV);
  332. }
  333.  
  334. #endif 
  335.  
  336. void
  337. PastBottom()
  338. {                /* used to go to last used screen line */
  339.     MoveToLine(OldvcV);
  340.     (void) putraw('\r');
  341.     (void) putraw('\n');
  342.     ClearDisp();
  343.     flush();
  344. }
  345.  
  346.  
  347. /* insert num characters of s into d (in front of the character) at dat,
  348.    maximum length of d is dlen */
  349. static void
  350. str_insert(d, dat, dlen, s, num)
  351.     register Char *d;
  352.     register int dat, dlen;
  353.     register Char *s;
  354.     register int num;
  355. {
  356.     register Char *a, *b;
  357.  
  358.     if (num <= 0)
  359.     return;
  360.     if (num > dlen - dat)
  361.     num = dlen - dat;
  362.  
  363. #ifdef DEBUG_REFRESH
  364.     dprintf("str_insert() starting: %d at %d max %d, d == \"%s\"\n",
  365.         num, dat, dlen, short2str(d));
  366.     dprintf("s == \"%s\"n", short2str(s));
  367. #endif /* DEBUG_REFRESH */
  368.  
  369.     /* open up the space for num chars */
  370.     if (num > 0) {
  371.     b = d + dlen - 1;
  372.     a = b - num;
  373.     while (a >= &d[dat])
  374.         *b-- = *a--;
  375.     d[dlen] = '\0';        /* just in case */
  376.     }
  377. #ifdef DEBUG_REFRESH
  378.     dprintf("str_insert() after insert: %d at %d max %d, d == \"%s\"\n",
  379.         num, dat, dlen, short2str(d));
  380.     dprintf("s == \"%s\"n", short2str(s));
  381. #endif /* DEBUG_REFRESH */
  382.  
  383.     /* copy the characters */
  384.     for (a = d + dat; (a < d + dlen) && (num > 0); num--)
  385.     *a++ = *s++;
  386.  
  387. #ifdef DEBUG_REFRESH
  388.     dprintf("str_insert() after copy: %d at %d max %d, d == \"%s\"\n",
  389.         num, dat, dlen, d, short2str(s));
  390.     dprintf("s == \"%s\"n", short2str(s));
  391. #endif /* DEBUG_REFRESH */
  392. }
  393.  
  394. /* delete num characters d at dat, maximum length of d is dlen */
  395. static void
  396. str_delete(d, dat, dlen, num)
  397.     register Char *d;
  398.     register int dat, dlen, num;
  399. {
  400.     register Char *a, *b;
  401.  
  402.     if (num <= 0)
  403.     return;
  404.     if (dat + num >= dlen) {
  405.     d[dat] = '\0';
  406.     return;
  407.     }
  408.  
  409. #ifdef DEBUG_REFRESH
  410.     dprintf("str_delete() starting: %d at %d max %d, d == \"%s\"\n",
  411.         num, dat, dlen, short2str(d));
  412. #endif /* DEBUG_REFRESH */
  413.  
  414.     /* open up the space for num chars */
  415.     if (num > 0) {
  416.     b = d + dat;
  417.     a = b + num;
  418.     while (a < &d[dlen])
  419.         *b++ = *a++;
  420.     d[dlen] = '\0';        /* just in case */
  421.     }
  422. #ifdef DEBUG_REFRESH
  423.     dprintf("str_delete() after delete: %d at %d max %d, d == \"%s\"\n",
  424.         num, dat, dlen, short2str(d));
  425. #endif /* DEBUG_REFRESH */
  426. }
  427.  
  428. static void
  429. str_cp(a, b, n)
  430.     register Char *a, *b;
  431.     register int n;
  432. {
  433.     while (n-- && *b)
  434.     *a++ = *b++;
  435. }
  436.  
  437.  
  438.  
  439. /* ****************************************************************
  440.     update_line() is based on finding the middle difference of each line
  441.     on the screen; vis:
  442.  
  443.                  /old first difference
  444.     /beginning of line   |              /old last same       /old EOL
  445.     v             v              v                    v
  446. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  447. new:    eddie> Oh, my little buggy says to me, as lurgid as
  448.     ^             ^        ^               ^
  449.     \beginning of line   |        \new last same       \new end of line
  450.                  \new first difference
  451.  
  452.     all are character pointers for the sake of speed.  Special cases for
  453.     no differences, as well as for end of line additions must be handled.
  454. **************************************************************** */
  455.  
  456. /* Minimum at which doing an insert it "worth it".  This should be about
  457.  * half the "cost" of going into insert mode, inserting a character, and
  458.  * going back out.  This should really be calculated from the termcap
  459.  * data...  For the moment, a good number for ANSI terminals.
  460.  */
  461. #define MIN_END_KEEP    4
  462.  
  463. static void            /* could be changed to make it smarter */
  464. update_line(old, new, cur_line)
  465.     register Char *old, *new;
  466.     int     cur_line;
  467. {
  468.     register Char *o, *n, *p, c;
  469.     Char   *ofd, *ols, *oe, *nfd, *nls, *ne;
  470.     Char   *osb, *ose, *nsb, *nse;
  471.     int     fx, sx;
  472.  
  473.     /*
  474.      * find first diff
  475.      */
  476.     for (o = old, n = new; *o && (*o == *n); o++, n++);
  477.     ofd = o;
  478.     nfd = n;
  479.  
  480.     /*
  481.      * Find the end of both old and new
  482.      */
  483.     while (*o)
  484.     o++;
  485.     /* 
  486.      * Remove any trailing blanks off of the end, being careful not to
  487.      * back up past the beginning.
  488.      */
  489.     while (ofd < o) {
  490.     if (o[-1] != ' ')
  491.         break;
  492.     o--;
  493.     }
  494.     oe = o;
  495.     *oe = (Char) 0;
  496.   
  497.     while (*n)
  498.     n++;
  499.  
  500.     /* remove blanks from end of new */
  501.     while (nfd < n) {
  502.     if (n[-1] != ' ')
  503.         break;
  504.     n--;
  505.     }
  506.     ne = n;
  507.     *ne = (Char) 0;
  508.   
  509.     /*
  510.      * if no diff, continue to next line of redraw
  511.      */
  512.     if (*ofd == '\0' && *nfd == '\0') {
  513. #ifdef DEBUG_UPDATE
  514.     dprintf("no difference.\r\n");
  515. #endif /* DEBUG_UPDATE */
  516.     return;
  517.     }
  518.  
  519.     /*
  520.      * find last same pointer
  521.      */
  522.     while ((o > ofd) && (n > nfd) && (*--o == *--n));
  523.     ols = ++o;
  524.     nls = ++n;
  525.  
  526.     /*
  527.      * find same begining and same end
  528.      */
  529.     osb = ols;
  530.     nsb = nls;
  531.     ose = ols;
  532.     nse = nls;
  533.  
  534.     /*
  535.      * case 1: insert: scan from nfd to nls looking for *ofd
  536.      */
  537.     if (*ofd) {
  538.     for (c = *ofd, n = nfd; n < nls; n++) {
  539.         if (c == *n) {
  540.         for (o = ofd, p = n; p < nls && o < ols && *o == *p; o++, p++);
  541.         /*
  542.          * if the new match is longer and it's worth keeping, then we
  543.          * take it
  544.          */
  545.         if (((nse - nsb) < (p - n)) && (2 * (p - n) > n - nfd)) {
  546.             nsb = n;
  547.             nse = p;
  548.             osb = ofd;
  549.             ose = o;
  550.         }
  551.         }
  552.     }
  553.     }
  554.  
  555.     /*
  556.      * case 2: delete: scan from ofd to ols looking for *nfd
  557.      */
  558.     if (*nfd) {
  559.     for (c = *nfd, o = ofd; o < ols; o++) {
  560.         if (c == *o) {
  561.         for (n = nfd, p = o; p < ols && n < nls && *p == *n; p++, n++);
  562.         /*
  563.          * if the new match is longer and it's worth keeping, then we
  564.          * take it
  565.          */
  566.         if (((ose - osb) < (p - o)) && (2 * (p - o) > o - ofd)) {
  567.             nsb = nfd;
  568.             nse = n;
  569.             osb = o;
  570.             ose = p;
  571.         }
  572.         }
  573.     }
  574.     }
  575. #ifdef notdef
  576.     /*
  577.      * If `last same' is before `same end' re-adjust
  578.      */
  579.     if (ols < ose)
  580.     ols = ose;
  581.     if (nls < nse)
  582.     nls = nse;
  583. #endif
  584.  
  585.     /*
  586.      * Pragmatics I: If old trailing whitespace or not enough characters to
  587.      * save to be worth it, then don't save the last same info.
  588.      */
  589.     if ((oe - ols) < MIN_END_KEEP) {
  590.     ols = oe;
  591.     nls = ne;
  592.     }
  593.  
  594.     /*
  595.      * Pragmatics II: if the terminal isn't smart enough, make the data dumber
  596.      * so the smart update doesn't try anything fancy
  597.      */
  598.  
  599.     /*
  600.      * fx is the number of characters we need to insert/delete: in the
  601.      * beginning to bring the two same begins together
  602.      */
  603.     fx = (nsb - nfd) - (osb - ofd);
  604.     /*
  605.      * sx is the number of characters we need to insert/delete: in the end to
  606.      * bring the two same last parts together
  607.      */
  608.     sx = (nls - nse) - (ols - ose);
  609.  
  610.     if (!T_CanIns) {
  611.     if (fx > 0) {
  612.         osb = ols;
  613.         ose = ols;
  614.         nsb = nls;
  615.         nse = nls;
  616.     }
  617.     if (sx > 0) {
  618.         ols = oe;
  619.         nls = ne;
  620.     }
  621.     if ((ols - ofd) < (nls - nfd)) {
  622.         ols = oe;
  623.         nls = ne;
  624.     }
  625.     }
  626.     if (!T_CanDel) {
  627.     if (fx < 0) {
  628.         osb = ols;
  629.         ose = ols;
  630.         nsb = nls;
  631.         nse = nls;
  632.     }
  633.     if (sx < 0) {
  634.         ols = oe;
  635.         nls = ne;
  636.     }
  637.     if ((ols - ofd) > (nls - nfd)) {
  638.         ols = oe;
  639.         nls = ne;
  640.     }
  641.     }
  642.  
  643.     /*
  644.      * Pragmatics III: make sure the middle shifted pointers are correct if
  645.      * they don't point to anything (we may have moved ols or nls).
  646.      */
  647.     /* if the change isn't worth it, don't bother */
  648.     /* was: if (osb == ose) */
  649.     if ((ose - osb) < MIN_END_KEEP) {
  650.     osb = ols;
  651.     ose = ols;
  652.     nsb = nls;
  653.     nse = nls;
  654.     }
  655.  
  656.     /*
  657.      * Now that we are done with pragmatics we recompute fx, sx
  658.      */
  659.     fx = (nsb - nfd) - (osb - ofd);
  660.     sx = (nls - nse) - (ols - ose);
  661.  
  662. #ifdef DEBUG_UPDATE
  663.     dprintf("\n");
  664.     dprintf("ofd %d, osb %d, ose %d, ols %d, oe %d\n",
  665.         ofd - old, osb - old, ose - old, ols - old, oe - old);
  666.     dprintf("nfd %d, nsb %d, nse %d, nls %d, ne %d\n",
  667.         nfd - new, nsb - new, nse - new, nls - new, ne - new);
  668.     dprintf("xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n");
  669.     dprintf("xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n");
  670.     dprintstr("old- oe", old, oe);
  671.     dprintstr("new- ne", new, ne);
  672.     dprintstr("old-ofd", old, ofd);
  673.     dprintstr("new-nfd", new, nfd);
  674.     dprintstr("ofd-osb", ofd, osb);
  675.     dprintstr("nfd-nsb", nfd, nsb);
  676.     dprintstr("osb-ose", osb, ose);
  677.     dprintstr("nsb-nse", nsb, nse);
  678.     dprintstr("ose-ols", ose, ols);
  679.     dprintstr("nse-nls", nse, nls);
  680.     dprintstr("ols- oe", ols, oe);
  681.     dprintstr("nls- ne", nls, ne);
  682. #endif /* DEBUG_UPDATE */
  683.  
  684.     /*
  685.      * CursorV to this line cur_line MUST be in this routine so that if we
  686.      * don't have to change the line, we don't move to it. CursorH to first
  687.      * diff char
  688.      */
  689.     MoveToLine(cur_line);
  690.  
  691.     /*
  692.      * at this point we have something like this:
  693.      * 
  694.      * /old                  /ofd    /osb               /ose    /ols     /oe
  695.      * v.....................v       v..................v       v........v
  696.      * eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as
  697.      * eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as
  698.      * ^.....................^     ^..................^       ^........^ 
  699.      * \new                  \nfd  \nsb               \nse     \nls    \ne
  700.      * 
  701.      * fx is the difference in length between the the chars between nfd and
  702.      * nsb, and the chars between ofd and osb, and is thus the number of
  703.      * characters to delete if < 0 (new is shorter than old, as above),
  704.      * or insert (new is longer than short).
  705.      *
  706.      * sx is the same for the second differences.
  707.      */
  708.  
  709.     /*
  710.      * if we have a net insert on the first difference, AND inserting the net
  711.      * amount ((nsb-nfd) - (osb-ofd)) won't push the last useful character
  712.      * (which is ne if nls != ne, otherwise is nse) off the edge of the screen
  713.      * (TermH) else we do the deletes first so that we keep everything we need
  714.      * to.
  715.      */
  716.  
  717.     /*
  718.      * if the last same is the same like the end, there is no last same part,
  719.      * otherwise we want to keep the last same part set p to the last useful
  720.      * old character
  721.      */
  722.     p = (ols != oe) ? oe : ose;
  723.  
  724.     /*
  725.      * if (There is a diffence in the beginning) && (we need to insert
  726.      * characters) && (the number of characters to insert is less than the term
  727.      * width) We need to do an insert! else if (we need to delete characters)
  728.      * We need to delete characters! else No insert or delete
  729.      */
  730.     if ((nsb != nfd) && fx > 0 && ((p - old) + fx <= TermH)) {
  731. #ifdef DEBUG_UPDATE
  732.     dprintf("first diff insert at %d...\r\n", nfd - new);
  733. #endif  /* DEBUG_UPDATE */
  734.     /*
  735.      * Move to the first char to insert, where the first diff is.
  736.      */
  737.     MoveToChar(nfd - new);
  738.     /*
  739.      * Check if we have stuff to keep at end
  740.      */
  741.     if (nsb != ne) {
  742. #ifdef DEBUG_UPDATE
  743.         dprintf("with stuff to keep at end\r\n");
  744. #endif  /* DEBUG_UPDATE */
  745.         /*
  746.          * insert fx chars of new starting at nfd
  747.          */
  748.         if (fx > 0) {
  749. #ifdef DEBUG_UPDATE
  750.         if (!T_CanIns)
  751.             dprintf("   ERROR: cannot insert in early first diff\n");
  752. #endif  /* DEBUG_UPDATE */
  753.         Insert_write(nfd, fx);
  754.         str_insert(old, ofd - old, TermH, nfd, fx);
  755.         }
  756.         /*
  757.          * write (nsb-nfd) - fx chars of new starting at (nfd + fx)
  758.          */
  759.         so_write(nfd + fx, (nsb - nfd) - fx);
  760.         str_cp(ofd + fx, nfd + fx, (nsb - nfd) - fx);
  761.     }
  762.     else {
  763. #ifdef DEBUG_UPDATE
  764.         dprintf("without anything to save\r\n");
  765. #endif  /* DEBUG_UPDATE */
  766.         so_write(nfd, (nsb - nfd));
  767.         str_cp(ofd, nfd, (nsb - nfd));
  768.         /*
  769.          * Done
  770.          */
  771.         return;
  772.     }
  773.     }
  774.     else if (fx < 0) {
  775. #ifdef DEBUG_UPDATE
  776.     dprintf("first diff delete at %d...\r\n", ofd - old);
  777. #endif  /* DEBUG_UPDATE */
  778.     /*
  779.      * move to the first char to delete where the first diff is
  780.      */
  781.     MoveToChar(ofd - old);
  782.     /*
  783.      * Check if we have stuff to save
  784.      */
  785.     if (osb != oe) {
  786. #ifdef DEBUG_UPDATE
  787.         dprintf("with stuff to save at end\r\n");
  788. #endif  /* DEBUG_UPDATE */
  789.         /*
  790.          * fx is less than zero *always* here but we check for code
  791.          * symmetry
  792.          */
  793.         if (fx < 0) {
  794. #ifdef DEBUG_UPDATE
  795.         if (!T_CanDel)
  796.             dprintf("   ERROR: cannot delete in first diff\n");
  797. #endif /* DEBUG_UPDATE */
  798.         DeleteChars(-fx);
  799.         str_delete(old, ofd - old, TermH, -fx);
  800.         }
  801.         /*
  802.          * write (nsb-nfd) chars of new starting at nfd
  803.          */
  804.         so_write(nfd, (nsb - nfd));
  805.         str_cp(ofd, nfd, (nsb - nfd));
  806.  
  807.     }
  808.     else {
  809. #ifdef DEBUG_UPDATE
  810.         dprintf("but with nothing left to save\r\n");
  811. #endif  /* DEBUG_UPDATE */
  812.         /*
  813.          * write (nsb-nfd) chars of new starting at nfd
  814.          */
  815.         so_write(nfd, (nsb - nfd));
  816. #ifdef DEBUG_REFRESH
  817.         dprintf("cleareol %d\n", (oe - old) - (ne - new));
  818. #endif  /* DEBUG_UPDATE */
  819.         ClearEOL((oe - old) - (ne - new));
  820.         /*
  821.          * Done
  822.          */
  823.         return;
  824.     }
  825.     }
  826.     else
  827.     fx = 0;
  828.  
  829.     if (sx < 0) {
  830. #ifdef DEBUG_UPDATE
  831.     dprintf("second diff delete at %d...\r\n", (ose - old) + fx);
  832. #endif  /* DEBUG_UPDATE */
  833.     /*
  834.      * Check if we have stuff to delete
  835.      */
  836.     /*
  837.      * fx is the number of characters inserted (+) or deleted (-)
  838.      */
  839.  
  840.     MoveToChar((ose - old) + fx);
  841.     /*
  842.      * Check if we have stuff to save
  843.      */
  844.     if (ols != oe) {
  845. #ifdef DEBUG_UPDATE
  846.         dprintf("with stuff to save at end\r\n");
  847. #endif  /* DEBUG_UPDATE */
  848.         /*
  849.          * Again a duplicate test.
  850.          */
  851.         if (sx < 0) {
  852. #ifdef DEBUG_UPDATE
  853.         if (!T_CanDel)
  854.             dprintf("   ERROR: cannot delete in second diff\n");
  855. #endif  /* DEBUG_UPDATE */
  856.         DeleteChars(-sx);
  857.         }
  858.  
  859.         /*
  860.          * write (nls-nse) chars of new starting at nse
  861.          */
  862.         so_write(nse, (nls - nse));
  863.     }
  864.     else {
  865. #ifdef DEBUG_UPDATE
  866.         dprintf("but with nothing left to save\r\n");
  867. #endif /* DEBUG_UPDATE */
  868.         so_write(nse, (nls - nse));
  869. #ifdef DEBUG_REFRESH
  870.         dprintf("cleareol %d\n", (oe - old) - (ne - new));
  871. #endif /* DEBUG_UPDATE */
  872.         ClearEOL((oe - old) - (ne - new));
  873.     }
  874.     }
  875.  
  876.     /*
  877.      * if we have a first insert AND WE HAVEN'T ALREADY DONE IT...
  878.      */
  879.     if ((nsb != nfd) && (osb - ofd) <= (nsb - nfd) && (fx == 0)) {
  880. #ifdef DEBUG_UPDATE
  881.     dprintf("late first diff insert at %d...\r\n", nfd - new);
  882. #endif /* DEBUG_UPDATE */
  883.  
  884.     MoveToChar(nfd - new);
  885.     /*
  886.      * Check if we have stuff to keep at the end
  887.      */
  888.     if (nsb != ne) {
  889. #ifdef DEBUG_UPDATE
  890.         dprintf("with stuff to keep at end\r\n");
  891. #endif /* DEBUG_UPDATE */
  892.         /* 
  893.          * We have to recalculate fx here because we set it
  894.          * to zero above as a flag saying that we hadn't done
  895.          * an early first insert.
  896.          */
  897.         fx = (nsb - nfd) - (osb - ofd);
  898.         if (fx > 0) {
  899.         /*
  900.          * insert fx chars of new starting at nfd
  901.          */
  902. #ifdef DEBUG_UPDATE
  903.         if (!T_CanIns)
  904.             dprintf("   ERROR: cannot insert in late first diff\n");
  905. #endif /* DEBUG_UPDATE */
  906.         Insert_write(nfd, fx);
  907.         str_insert(old, ofd - old, TermH, nfd, fx);
  908.         }
  909.  
  910.         /*
  911.          * write (nsb-nfd) - fx chars of new starting at (nfd + fx)
  912.          */
  913.         so_write(nfd + fx, (nsb - nfd) - fx);
  914.         str_cp(ofd + fx, nfd + fx, (nsb - nfd) - fx);
  915.     }
  916.     else {
  917. #ifdef DEBUG_UPDATE
  918.         dprintf("without anything to save\r\n");
  919. #endif /* DEBUG_UPDATE */
  920.         so_write(nfd, (nsb - nfd));
  921.         str_cp(ofd, nfd, (nsb - nfd));
  922.     }
  923.     }
  924.  
  925.     /*
  926.      * line is now NEW up to nse
  927.      */
  928.     if (sx >= 0) {
  929. #ifdef DEBUG_UPDATE
  930.     dprintf("second diff insert at %d...\r\n", nse - new);
  931. #endif /* DEBUG_UPDATE */
  932.     MoveToChar(nse - new);
  933.     if (ols != oe) {
  934. #ifdef DEBUG_UPDATE
  935.         dprintf("with stuff to keep at end\r\n");
  936. #endif /* DEBUG_UPDATE */
  937.         if (sx > 0) {
  938.         /* insert sx chars of new starting at nse */
  939. #ifdef DEBUG_UPDATE
  940.         if (!T_CanIns)
  941.             dprintf("   ERROR: cannot insert in second diff\n");
  942. #endif /* DEBUG_UPDATE */
  943.         Insert_write(nse, sx);
  944.         }
  945.  
  946.         /*
  947.          * write (nls-nse) - sx chars of new starting at (nse + sx)
  948.          */
  949.         so_write(nse + sx, (nls - nse) - sx);
  950.     }
  951.     else {
  952. #ifdef DEBUG_UPDATE
  953.         dprintf("without anything to save\r\n");
  954. #endif /* DEBUG_UPDATE */
  955.         so_write(nse, (nls - nse));
  956.  
  957.         /*
  958.              * No need to do a clear-to-end here because we were doing
  959.          * a second insert, so we will have over written all of the
  960.          * old string.
  961.          */
  962.     }
  963.     }
  964. #ifdef DEBUG_UPDATE
  965.     dprintf("done.\r\n");
  966. #endif /* DEBUG_UPDATE */
  967. }
  968.  
  969.  
  970. static void
  971. cpy_pad_spaces(dst, src, width)
  972.     register Char *dst, *src;
  973.     register int width;
  974. {
  975.     register int i;
  976.  
  977.     for (i = 0; i < width; i++) {
  978.     if (*src == (Char) 0)
  979.         break;
  980.     *dst++ = *src++;
  981.     }
  982.  
  983.     while (i < width) {
  984.     *dst++ = ' ';
  985.     i++;
  986.     }
  987.     *dst = (Char) 0;
  988. }
  989.  
  990. void
  991. RefCursor()
  992. {                /* only move to new cursor pos */
  993.     register Char *cp, c;
  994.     register int h, th, v;
  995.  
  996.     if (DoingSearch) {
  997.     Refresh();
  998.     return;
  999.     }
  1000.  
  1001.     /* first we must find where the cursor is... */
  1002.     h = 0;
  1003.     v = 0;
  1004.     th = TermH;            /* optimize for speed */
  1005.  
  1006.     for (cp = PromptBuf; *cp; cp++) {    /* do prompt */
  1007.     if (*cp & LITERAL)
  1008.         continue;
  1009.     c = *cp & CHAR;        /* extra speed plus strip the inverse */
  1010.     h++;            /* all chars at least this long */
  1011.  
  1012.     /* from wolman%crltrx.DEC@decwrl.dec.com (Alec Wolman) */
  1013.     /* lets handle newline as part of the prompt */
  1014.  
  1015.     if (c == '\n') {
  1016.         h = 0;
  1017.         v++;
  1018.     }
  1019.     else {
  1020.         if (c == '\t') {    /* if a tab, to next tab stop */
  1021.         while (h & 07) {
  1022.             h++;
  1023.         }
  1024.         }
  1025.         else if (Iscntrl(c)) {    /* if control char */
  1026.         h++;
  1027.         if (h > th) {    /* if overflow, compensate */
  1028.             h = 1;
  1029.             v++;
  1030.         }
  1031.         }
  1032.         else if (!Isprint(c)) {
  1033.         h += 3;
  1034.         if (h > th) {    /* if overflow, compensate */
  1035.             h = h - th;
  1036.             v++;
  1037.         }
  1038.         }
  1039.     }
  1040.  
  1041.     if (h >= th) {        /* check, extra long tabs picked up here also */
  1042.         h = 0;
  1043.         v++;
  1044.     }
  1045.     }
  1046.  
  1047.     for (cp = InputBuf; cp < Cursor; cp++) {    /* do input buffer to Cursor */
  1048.     c = *cp & CHAR;        /* extra speed plus strip the inverse */
  1049.     h++;            /* all chars at least this long */
  1050.  
  1051.     if (c == '\n') {    /* handle newline in data part too */
  1052.         h = 0;
  1053.         v++;
  1054.     }
  1055.     else {
  1056.         if (c == '\t') {    /* if a tab, to next tab stop */
  1057.         while (h & 07) {
  1058.             h++;
  1059.         }
  1060.         }
  1061.         else if (Iscntrl(c)) {    /* if control char */
  1062.         h++;
  1063.         if (h > th) {    /* if overflow, compensate */
  1064.             h = 1;
  1065.             v++;
  1066.         }
  1067.         }
  1068.         else if (!Isprint(c)) {
  1069.         h += 3;
  1070.         if (h > th) {    /* if overflow, compensate */
  1071.             h = h - th;
  1072.             v++;
  1073.         }
  1074.         }
  1075.     }
  1076.  
  1077.     if (h >= th) {        /* check, extra long tabs picked up here also */
  1078.         h = 0;
  1079.         v++;
  1080.     }
  1081.     }
  1082.  
  1083.     /* now go there */
  1084.     MoveToLine(v);
  1085.     MoveToChar(h);
  1086.     flush();
  1087. }
  1088.  
  1089. static void
  1090. PutPlusOne(c)
  1091.     int    c;
  1092. {
  1093.     (void) putraw(c);
  1094.     Display[CursorV][CursorH++] = c;
  1095.     if (CursorH >= TermH) {    /* if we must overflow */
  1096.     CursorH = 0;
  1097.     CursorV++;
  1098.     OldvcV++;
  1099.     (void) putraw('\r');
  1100.     (void) putraw('\n');
  1101.     }
  1102. }
  1103.  
  1104. void
  1105. RefPlusOne()
  1106. {                /* we added just one char, handle it fast *//* a
  1107.                  * ssumes that screen cursor == real cursor */
  1108.     register Char c, mc;
  1109.  
  1110.     c = Cursor[-1] & CHAR;    /* the char we just added */
  1111.  
  1112.     if (c == '\t' || Cursor != LastChar) {
  1113.     Refresh();        /* too hard to handle */
  1114.     return;
  1115.     }                /* else (only do at end of line, no TAB) */
  1116.  
  1117.     if (DoingSearch) {
  1118.     Refresh();
  1119.     return;
  1120.     }
  1121.  
  1122.     if (Iscntrl(c)) {        /* if control char, do caret */
  1123.     mc = (c == '\177') ? '?' : (c | 0100);
  1124.     PutPlusOne('^');
  1125.     PutPlusOne(mc);
  1126.     }
  1127.     else if (Isprint(c)) {    /* normal char */
  1128.     PutPlusOne(c);
  1129.     }
  1130.     else {
  1131.     PutPlusOne('\\');
  1132.     PutPlusOne(((c >> 6) & 7) + '0');
  1133.     PutPlusOne(((c >> 3) & 7) + '0');
  1134.     PutPlusOne((c & 7) + '0');
  1135.     }
  1136.     flush();
  1137. }
  1138.  
  1139. /* clear the screen buffers so that new new prompt starts fresh. */
  1140.  
  1141. void
  1142. ClearDisp()
  1143. {
  1144.     register int i;
  1145.  
  1146.     CursorV = 0;        /* clear the display buffer */
  1147.     CursorH = 0;
  1148.     for (i = 0; i < TermV; i++)
  1149.     Display[i][0] = '\0';
  1150.     OldvcV = 0;
  1151. }
  1152.  
  1153. void
  1154. ClearLines()
  1155. {                /* Make sure all lines are *really* blank */
  1156.     register int i;
  1157.  
  1158.     if (T_CanCEOL) {
  1159.     MoveToChar(0);
  1160.     for (i = 0; i <= OldvcV; i++) {    /* for each line on the screen */
  1161.         MoveToLine(i);
  1162.         ClearEOL(TermH);
  1163.     }
  1164.     MoveToLine(0);
  1165.     }
  1166.     else {
  1167.     MoveToLine(OldvcV);    /* go to last line */
  1168.     (void) putraw('\r');    /* go to BOL */
  1169.     (void) putraw('\n');    /* go to new line */
  1170.     }
  1171. }
  1172.